package com.lizard.schedule.wear;

import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.Wearable;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lizard.schedule.ScheduleApplication;
import com.lizard.schedule.manager.AccountManager;
import com.lizard.schedule.persistence.db.bean.Schedule;
import com.lizard.schedule.persistence.db.table.ScheduleTbl;
import com.lizard.schedule.ui.adapter.schedule.ScheduleComparetor;
import com.lizard.schedule.ui.adapter.schedule.ScheduleFacade;
import com.lizard.schedule.ui.adapter.schedule.ScheduleListAdapter;
import com.lizard.schedule.wear.consts.Path;
import com.lizard.schedule.wear.model.WearSchedule;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by wangyiqian on 16/6/8.
 */

public class Sender {

    /**
     * 发送计划列表给wear
     */
    public synchronized static void sendSchedules(){

        new Thread(new Runnable() {
            @Override
            public void run() {

                GoogleApiClient googleApiClient = new GoogleApiClient.Builder(ScheduleApplication.applicationContext)
                        .addApi(Wearable.API)
                        .build();

                ConnectionResult connectionResult = googleApiClient.blockingConnect(5, TimeUnit.SECONDS);

                if(connectionResult != null && connectionResult.isSuccess()){
                    sendSchedules(googleApiClient);
                    googleApiClient.disconnect();
                }

            }
        }).start();

    }

    private static void sendSchedules(GoogleApiClient googleApiClient){
        // 查询数据
        List<Schedule> schedules = ScheduleTbl.getInstance().select(
                AccountManager.getInstance().getmUsernameMD5L16(), 0, 1000, System.currentTimeMillis(), true, false);
        if(schedules != null){

            ScheduleComparetor scheduleComparetor = new ScheduleComparetor(ScheduleListAdapter.SORT_BY_FUTURE);
            List<ScheduleFacade> scheduleFacades = new ArrayList<ScheduleFacade>();
            for(Schedule schedule: schedules){
                scheduleFacades.add(new ScheduleFacade(schedule));
            }
            Collections.sort(scheduleFacades, scheduleComparetor);

            List<WearSchedule> wearSchedules = new ArrayList<>();
            for(ScheduleFacade scheduleFacade: scheduleFacades){
                WearSchedule wearSchedule = new WearSchedule();
                wearSchedule.setLocalId(scheduleFacade.getSchedule().getLocalId());
                wearSchedule.setBeginTime(scheduleFacade.getSchedule().getBeginTime());
                wearSchedule.setContentText(scheduleFacade.getSchedule().getContentText());
                wearSchedule.setDateStr(scheduleFacade.getDateStr());
                wearSchedule.setDayNearby(scheduleFacade.getDayNearby());
                wearSchedule.setDayOfWeekName(scheduleFacade.getDayOfWeekName());
                wearSchedule.setStatus(scheduleFacade.getSchedule().getStatus());
                wearSchedule.setTimeStr(scheduleFacade.getTimeStr());
                wearSchedules.add(wearSchedule);
            }

            Type type = new TypeToken<List<WearSchedule>>(){}.getType();
            String data = new Gson().toJson(wearSchedules, type);

            PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(Path.CMD_SEND_SCHEDULES);
            putDataMapRequest.getDataMap().putString("timestamp", System.currentTimeMillis() + "");
            putDataMapRequest.getDataMap().putString("data", data);
            PendingResult pendingResult = Wearable.DataApi.putDataItem(googleApiClient, putDataMapRequest.asPutDataRequest());
        }
    }

}
